package com.briup.cms.config;


import com.briup.cms.web.interceptor.JWTInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private JWTInterceptor jwtInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //多个拦截器，拦截顺序和注册顺序有关
        //可以使用order(num) 指定顺序 num越大越靠后

        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")////任意请求路径被拦截
                .excludePathPatterns("/login",
                                     "/animal/findByAnimalId",//客户通过二维码直接访问资源，不需要认证信息
                                     "/swagger-ui.html",
                                     "/swagger-resources/**",
                                    "/error",
                                    "/webjars/**")//排除登录的请求拦截
                .order(1);//排除登录请求不拦截
    }
    //设置跨域访问的规则

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 设置允许的请求方式
                .allowedMethods("GET", "POST", "DELETE",
                        "PUT","HEAD","OPTIONS")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 跨域允许时间
                .maxAge(3600);
    }
}
